﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AStartAlgorithm.Comm
{
    public class ArrayParse
    {
        /// <summary>
        /// 数组->向量行
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="vec"></param>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <returns></returns>
        public static T[,] Row2ArrT<T>(T[] vec, int col, int row)
        {
            T[,] ret = new T[col, row];
            for (int x = 0; x < col; x++)
            {
                for (int y = 0; y < row; y++)
                {
                    ret[x, y] = vec[x + y * col];
                }
            }
            return ret;
        }

        /// <summary>
        /// 数组->向量行
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="vec"></param>
        /// <param name="template"></param>
        /// <returns></returns>
        public static T[,] Row2ArrT<T>(T[] vec, T[,] template)
        {
            return Row2ArrT(vec, template.GetLength(0), template.GetLength(1));
        }

        /// <summary>
        /// 向量行->数组
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="vecData"></param>
        /// <returns></returns>
        public static T[] Arr2RowT<T>(T[,] vecData)
        {
            int col = vecData.GetLength(0);
            int row = vecData.GetLength(1);
            T[] datas = new T[row * col];
            for (int x = 0; x < col; x++)
            {
                for (int y = 0; y < row; y++)
                {
                    datas[y * col + x] = vecData[x, y];
                }
            }
            return datas;
        }
    }
}
